package com.cy.aircompressor.entity.sensor;

import com.cy.aircompressor.entity.linechart.LineChartEntry;
import com.drug.lib.view.recycptr.bean.BaseEntity;
import com.github.mikephil.charting.data.Entry;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class SensorAgg extends BaseEntity {
    private Sensor sensor;
    private List<SensorStatisticsData> sensorStatisticsData;
    private LineChartEntry lineChartEntry;

    public LineChartEntry getLineChartEntry() {
        if (lineChartEntry == null) {
            lineChartEntry = new LineChartEntry();
        }
        return lineChartEntry;
    }

    public void setLineChartEntry(LineChartEntry lineChartEntry) {
        this.lineChartEntry = lineChartEntry;
    }

    public Sensor getSensor() {
        return sensor;
    }

    public void setSensor(Sensor sensor) {
        this.sensor = sensor;
    }

    public List<SensorStatisticsData> getSensorStatisticsData() {
        return sensorStatisticsData;
    }

    public void setSensorStatisticsData(List<SensorStatisticsData> sensorStatisticsData) {
        this.sensorStatisticsData = sensorStatisticsData;
    }


    private List<Entry> getChartEntry() {
        List<Entry> entries = new ArrayList<>();
        if (sensorStatisticsData != null && sensorStatisticsData.size() != 0) {
            for (int i = 0; i < sensorStatisticsData.size(); i++) {
                SensorStatisticsData statisticsData = sensorStatisticsData.get(i);
                long time = getDate(statisticsData.getRt());
                Entry entry = new Entry(i, (float) statisticsData.getV(), time);
                entries.add(entry);
            }
        }
        return entries;
    }

    private long getDate(String rt) {
        Date date = null;
        try {
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            date = format.parse(rt);
            return date.getTime();
        } catch (ParseException e) {
            e.printStackTrace();
        }

        return 0;
    }
}
